<?php
class ProjectsController extends AppController {

	var $name = 'Projects';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Project->recursive = 0;
		$this->set('projects', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Project.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('project', $this->Project->read(null, $id));
	}

	function add() {
		$this->set('walle', $this->data);
		if (!empty($this->data)) {
			if ($this->data['Project']['proj_main_logo1']['error']) {
				switch ($this->data['Project']['proj_main_logo1']['error']){
					case 1: // UPLOAD_ERR_INI_SIZE
						$this->Session->setFlash(__('Le fichier dépasse la limite autorisée par le serveur (fichier php.ini) !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 2: // UPLOAD_ERR_FORM_SIZE
						$this->Session->setFlash(__('Le fichier dépasse la limite autorisée dans le formulaire HTML !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 3: // UPLOAD_ERR_PARTIAL
						$this->Session->setFlash(__('L\'envoi du fichier a été interrompu pendant le transfert !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 4: // UPLOAD_ERR_NO_FILE
						$this->Session->setFlash(__('Le fichier que vous avez envoyé a une taille nulle !', true));
						$this->redirect(array('action'=>'index'));
						break;
				}
			}

			if ($this->data['Project']['proj_alt_logo1']['error']) {
				switch ($this->data['Project']['proj_alt_logo1']['error']){
					case 1: // UPLOAD_ERR_INI_SIZE
						$this->Session->setFlash(__('Le fichier dépasse la limite autorisée par le serveur (fichier php.ini) !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 2: // UPLOAD_ERR_FORM_SIZE
						$this->Session->setFlash(__('Le fichier dépasse la limite autorisée dans le formulaire HTML !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 3: // UPLOAD_ERR_PARTIAL
						$this->Session->setFlash(__('L\'envoi du fichier a été interrompu pendant le transfert !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 4: // UPLOAD_ERR_NO_FILE
						$this->Session->setFlash(__('Le fichier que vous avez envoyé a une taille nulle !', true));
						$this->redirect(array('action'=>'index'));
						break;
				}
			}
			if ($this->data['Project']['proj_main_logo1']['error'] == UPLOAD_ERR_OK) {
				$chemin_destination = '../webroot/img/COMPANY/'.$this->data['Project']['proj_company'].'/';
				//				echo "VERS : ".$chemin_destination.$this->data['Project']['proj_main_logo1']['name'];
				unlink($chemin_destination.$this->data['Project']['proj_main_logo1']['name']);
				touch($chemin_destination.$this->data['Project']['proj_main_logo1']['name']);
				move_uploaded_file($this->data['Project']['proj_main_logo1']['tmp_name'], $chemin_destination.$this->data['Project']['proj_main_logo1']['name']);
			}

			if ($this->data['Project']['proj_alt_logo1']['error'] == UPLOAD_ERR_OK) {
				$chemin_destination = '../webroot/img/COMPANY/'.$this->data['Project']['proj_company'].'/';
				//				echo "VERS2 : ".$chemin_destination.$this->data['Project']['proj_alt_logo1']['name'];
				unlink($chemin_destination.$this->data['Project']['proj_alt_logo1']['name']);
				touch($chemin_destination.$this->data['Project']['proj_alt_logo1']['name']);
				move_uploaded_file($this->data['Project']['proj_alt_logo1']['tmp_name'], $chemin_destination.$this->data['Project']['proj_alt_logo1']['name']);
			}


			$this->Project->create();
			$this->data['Project']['proj_main_logo'] = 'img/COMPANY/'.$this->data['Project']['proj_company'].'/'.$this->data['Project']['proj_main_logo1']['name'] ;
			$this->data['Project']['proj_alt_logo'] = 'img/COMPANY/'.$this->data['Project']['proj_company'].'/'.$this->data['Project']['proj_alt_logo1']['name'] ;
			if ($this->Project->save($this->data)) {
				$this->Session->setFlash(__('The Project has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Project could not be saved. Please, try again.', true));
			}
		}
		$companies = $this->Project->Company->find('list');
		$this->set(compact('companies'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Project', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->data['Project']['proj_main_logo1']['error']) {
				switch ($this->data['Project']['proj_main_logo1']['error']){
					case 1: // UPLOAD_ERR_INI_SIZE
						$this->Session->setFlash(__('Le fichier dépasse la limite autorisée par le serveur (fichier php.ini) !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 2: // UPLOAD_ERR_FORM_SIZE
						$this->Session->setFlash(__('Le fichier dépasse la limite autorisée dans le formulaire HTML !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 3: // UPLOAD_ERR_PARTIAL
						$this->Session->setFlash(__('L\'envoi du fichier a été interrompu pendant le transfert !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 4: // UPLOAD_ERR_NO_FILE
						$this->Session->setFlash(__('Le fichier que vous avez envoyé a une taille nulle !', true));
						$this->redirect(array('action'=>'index'));
						break;
				}
			}

			if ($this->data['Project']['proj_alt_logo1']['error']) {
				switch ($this->data['Project']['proj_alt_logo1']['error']){
					case 1: // UPLOAD_ERR_INI_SIZE
						$this->Session->setFlash(__('Le fichier dépasse la limite autorisée par le serveur (fichier php.ini) !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 2: // UPLOAD_ERR_FORM_SIZE
						$this->Session->setFlash(__('Le fichier dépasse la limite autorisée dans le formulaire HTML !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 3: // UPLOAD_ERR_PARTIAL
						$this->Session->setFlash(__('L\'envoi du fichier a été interrompu pendant le transfert !', true));
						$this->redirect(array('action'=>'index'));
						break;
					case 4: // UPLOAD_ERR_NO_FILE
						$this->Session->setFlash(__('Le fichier que vous avez envoyé a une taille nulle !', true));
						$this->redirect(array('action'=>'index'));
						break;
				}
			}
			if ($this->data['Project']['proj_main_logo1']['error'] == UPLOAD_ERR_OK) {
				$chemin_destination = '../webroot/img/COMPANY/'.$this->data['Project']['proj_company'].'/';
				//				echo "VERS : ".$chemin_destination.$this->data['Project']['proj_main_logo1']['name'];
				unlink($chemin_destination.$this->data['Project']['proj_main_logo1']['name']);
				touch($chemin_destination.$this->data['Project']['proj_main_logo1']['name']);
				move_uploaded_file($this->data['Project']['proj_main_logo1']['tmp_name'], $chemin_destination.$this->data['Project']['proj_main_logo1']['name']);
			}

			if ($this->data['Project']['proj_alt_logo1']['error'] == UPLOAD_ERR_OK) {
				$chemin_destination = '../webroot/img/COMPANY/'.$this->data['Project']['proj_company'].'/';
				//				echo "VERS2 : ".$chemin_destination.$this->data['Project']['proj_alt_logo1']['name'];
				unlink($chemin_destination.$this->data['Project']['proj_alt_logo1']['name']);
				touch($chemin_destination.$this->data['Project']['proj_alt_logo1']['name']);
				move_uploaded_file($this->data['Project']['proj_alt_logo1']['tmp_name'], $chemin_destination.$this->data['Project']['proj_alt_logo1']['name']);
			}
				
				
			$this->data['Project']['proj_main_logo'] = 'img/COMPANY/'.$this->data['Project']['proj_company'].'/'.$this->data['Project']['proj_main_logo1']['name'] ;
			$this->data['Project']['proj_alt_logo'] = 'img/COMPANY/'.$this->data['Project']['proj_company'].'/'.$this->data['Project']['proj_alt_logo1']['name'] ;
			$this->data['Project']['proj_index'] = $id ;
			if ($this->Project->save($this->data)) {
				$this->Session->setFlash(__('The Project has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Project could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Project->read(null, $id);
		}
		$companies = $this->Project->Company->find('list');
		$this->set(compact('companies'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Project', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Project->del($id)) {
			$this->Session->setFlash(__('Project deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}


	function admin_index() {
		$this->Project->recursive = 0;
		$this->set('projects', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Project.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('project', $this->Project->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Project->create();
			if ($this->Project->save($this->data)) {
				$this->Session->setFlash(__('The Project has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Project could not be saved. Please, try again.', true));
			}
		}
		$companies = $this->Project->Company->find('list');
		$this->set(compact('companies'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Project', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Project->save($this->data)) {
				$this->Session->setFlash(__('The Project has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Project could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Project->read(null, $id);
		}
		$companies = $this->Project->Company->find('list');
		$this->set(compact('companies'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Project', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Project->del($id)) {
			$this->Session->setFlash(__('Project deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

	function choose() {
		$this->set('projects_name', array( 0 => 'Sélectionnez l\'entreprise avant'));
		if (!empty($this->data['Project']['proj_company'])) {
			$this->Session->write('company_name', $this->data['Project']['proj_company'] ) ;
			$idProj = $this->data['Project']['proj_company'];
			$this->set('projects_name', $this->Project->find('list', array ('fields' => array('Project.proj_index', 'Project.proj_name'), 'conditions' => array('proj_company' => $idProj))));
		}
		$this->Project->recursive = 0;

		$this->set('companies', $this->Project->Company->find('list'));
		$this->set('projects', $this->paginate());
		$this->set(compact('companies', 'projects_name'));

	}

	function select()
	{
		if (!empty($this->data)) {
			$this->Session->write('id_projet', $this->data['Project']['proj_name'] ) ;
			$this->flash('Projet sélectionné', '/procedures/index');
		}
		else
		{
			$this->flash('Erreur de sélection', '/projects/choose');
		}
	}

}
?>